<?php
//
//  VictoryCMS - Content managment system and framework.
//  Copyright (C) 2009  Lewis Gunsch
//
//  This file is part of VictoryCMS.
//  
//  VictoryCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  VictoryCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with VictoryCMS.  If not, see <http://www.gnu.org/licenses/>.

/**
 * VictoryCMS - VcmsPgSQL
 * 
 * @license http://www.gnu.org/licenses/gpl.html
 * @author Lewis Gunsch
 * @filesource
 * @package DBAbstraction
 */

/**
 * The functions necessary to interface with a
 * VictoryCMS Postgres database.
 * 
 * @see VcmsDB
 * 
 * @package DBAbstraction
 * 
 */
class VcmsPgSQL extends VcmsDB {

	/**
	 * @see VcmsDB#__construct()
	 **/
	public function __construct() {
		parent::__construct();
	}

	/**
	 * @see VcmsDB#__destruct()
	 */
	public function __destruct() {
		parent::__destruct();
	}
	
	/**
	 * @see VcmsDB#getConnected()
	 * 
	 * @todo FIX ME, the connection string must be built more intelligently 
	 * to account for parameters not being set!
	 */
	public function getConnected() {
		if (empty($this->connection) || !is_resource($this->connection)) {
			$settings = VcmsSettings::getInstance();
			$host = $settings->getDbHostname();
			$userName = $settings->getDbUsername();
			$password = $settings->getDbPassword();
			$dbName = $settings->getDbName();
			$this->connection = pg_connect("host=$host dbname=$dbName user=$userName password=$password");
			if (! is_resource($this->connection)) {
				throw new Exception( "Could not connect to database at $host: " . pg_errormessage() );
			}
		}
	}
	
	/**
	 * @see include/database_abstraction_layer/VcmsDB#isConnected()
	 */
	public function isConnected() {
		if (empty($this->connection)) {
			return false;
		}
		return is_resource($this->connection);
	}
	
	/**
	 * @see VcmsDB#cleanData()
	 * 
	 * @todo cleanData is not implemented yet
	 */
	public function cleanData($data) {
		return $data;
	}
	
	/**
	 * @see VcmsDB#runQuery()
	 */
	public function runQuery($queryName, SqlSet $querySet, array $dataSet = NULL) {
		//if (empty($this->connection) || !is_resource($this->connection)) {
			$this->getConnected();
		//}
		$settings = VcmsSettings::getInstance();
		if ($dataSet == NULL) {
			$query = $querySet->getAssembledQuery($queryName);
		} else {
			$query = $querySet->getAssembledQuery($queryName, $dataSet);
		}
		$this->result = pg_query($this->connection, $query);
		if (!$this->result) {
			throw new Exception("Invalid database query: " . pg_errormessage());
		}
	}
	
	/**
	 * @see VcmsDB#fetchResults()
	 */
	public function fetchRow($row = -1) {
		if (! isset($this->result)) {
			return false;
		}
		if (is_resource($this->result)) {
			if ($row >= 0) {
				$row = pg_fetch_array( $this->result, $row );
			} else {
				$row = pg_fetch_array( $this->result );
			}
		} else {
			return $this->result;
		}
		return $row;
	}
	
	/**
	 * @see include/database_abstraction_layer/VcmsDB#fetchFieldName()
	 */
	public function fetchFieldName($offSet) {
		if (! isset($this->result)) {
			return false;
		}
		if (is_resource($this->result) && is_numeric($offSet) 
			&& $offSet >= 0) {
				$name = pg_field_name( $this->result, $offSet );
		} else {
			return false;
		}
		return $name;
	}
	
	/**
	 * @see include/database_abstraction_layer/VcmsDB#fetchFieldType()
	 */
	public function fetchFieldType($offSet) {
		if (! isset($this->result)) {
			return false;
		}
		if (is_resource($this->result) && is_numeric($offSet) 
			&& $offSet >= 0) {
			$name = pg_field_type( $this->result, $offSet );
		} else {
			return false;
		}
		return $name;
	}
	
	/**
	 * @see include/database_abstraction_layer/VcmsDB#fetchNumRows()
	 */
	public function fetchNumRows() {
		if (! isset($this->result)) {
			return -1;
		}
		if (is_resource($this->result)) {
			$count = pg_num_rows($this->result );
		} else {
			return -1;
		}
		return $count;
	}
	
	/**
	 * @see include/database_abstraction_layer/VcmsDB#fetchNumFields()
	 */
	public function fetchNumFields() {
		if (! isset($this->result)) {
			return -1;
		}
		if (is_resource($this->result)) {
			$count = pg_num_fields($this->result );
		} else {
			return -1;
		}
		return $count;
	}
	
	/**
	 * @see VcmsDB#cleanUp()
	 */
	public function cleanUp() {
		if (!empty($this->result)) {
			if(is_resource($this->connection)) {
				pg_close($this->connection);
			}
		}
		if (!empty($this->connection)) {
			if (is_resource($this->result)) {
				pg_free_result($this->result);
			}
		}
		unset($this->result);
		unset($this->connection);
	}
	
}
?>